(function () {
    $.fn.extend({
        select() { //打钩
            this.find("i").click(function () {
                $(this).addClass("sel").siblings().removeClass("sel")
            })
        },
        tabChange() { //tab切换
            let goodsInfo = this.find(".goodsInfo")
            let upLis = this.find(".navList li");
            let downLis = this.find("ol li");
            [...upLis].forEach((el, index) => {
                el.onclick = () => {
                    $(goodsInfo).hide();
                    [...upLis].forEach((el, tmpIndex) => {
                        downLis[tmpIndex].className = el.className = index == tmpIndex ? `current` : "";
                    })
                }
            })
        },
        clic() { //加背景图
            this.find("li").click(function () {
                $(this).addClass("clic").siblings().removeClass("clic");
            })

        },
    })
})()
//弹窗
$(function () {
    $("#addCart").click(function () {
        layer.open({
            type: 1,
            title: "温馨提示",
            // skin: 'layui-layer-rim', //加上边框
            // area: ['600px', '400px'], //宽高
            content: `
            <div class="infoCart">
            <h2>已成功加入购物车</h2>
            <div>
                <a href="">继续购物</a>
                <a href="./cart.html">去购物车结算</a>
            </div>
        </div>
           `
        });
    })
})
$(function () {
    $("img.lazy").lazyload({
        effect: "fadeIn"
    }); //懒加载
    $(".mineBar").select() //打钩
    $("section").tabChange() //tab切换
    $(".downNavBox").clic() //加背景图

    //点击加入购物车按钮,购物车数量+1
    let num = 0;
    $("#addCart").click(function () {
        num++
        $(".cartNum").text("(" + num + ")")
    })
    //登录完成修改登录信息
    let userdata = JSON.parse(localStorage.getItem("userInfo") || '{}')
    console.log(userdata);
    $(".right-nav").find("span").html(`
        <span>欢迎你,${userdata.uname}<a class="logout" href="JavaScript:void(0)">退出</a></span>
    `)
    //点商品跳转到放大镜
    $("ol li .listwrap").find("dl").click(function () {
        location = "./list.html"
    })


    //动态传参到放大镜部分
    let gPruduction;
    let id = location.search.split("?")[1].split("=")[1]
    $.ajax({
        url: `http://127.0.0.1:8080/api/prodinfo/${id}`
    }).done(function (res) {
        console.log(res);
        $("#mImg").attr("src", res.data.img);
        $("#bigImg").attr("src", res.data.img);
        $("#pname").text(res.data.pname)
        $("#pprice").text(res.data.pprice)
        $("#wprice").text(res.data.pprice * 0.97)
        // $("#pname").text(res.data.pname)
        gPruduction = res.data;
    })

     //加入购物车
     $(function () {
        $("#addCart").on("click", function () {
            let data = Object.assign({
                uId: JSON.parse(localStorage.getItem("userInfo")).uId, //购买人id
            }, gPruduction);

            data.pimg = data.img;
            delete(data.img);
            // data.pnum = $("#pnum").val();
            data.pnum = 1;
            // console.log(data);
            $.ajax({
                url: "http://127.0.0.1:8080/api/AddCarts",
                type: 'post',
                data
            }).done(function (res) {
                console.log(res);
            })

        })
    })

})